package com.pdf.utils;

import com.domain.PdfUser;
import com.fonts.AbstractFont;
import com.fonts.DefaultFont;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import lombok.Data;

import java.io.*;
import java.util.Random;

/**
 * 添加图片水印
 */
@Data
public class ImgWatermark extends AbstractWatermark {
    private BaseFont baseFont;
    private String picWatermarkPath;
    private String userPassword;
    private String adminPassword;
    private InputStream watermarkStream;
    public static final Rectangle A4 = new RectangleReadOnly(842, 595);

    public ImgWatermark(Class baseFontClassName) {
        this(ImgWatermark.class.getResourceAsStream("/pic/abc.png"), baseFontClassName.getName());
    }

    public ImgWatermark() {
        this(ImgWatermark.class.getResourceAsStream("/pic/abc.png"));
    }

    public ImgWatermark(InputStream watermarkStream) {
        this(watermarkStream, DefaultFont.class.getName());
    }

    public ImgWatermark(InputStream watermarkStream, String baseFontClassName) {
        this.watermarkStream = watermarkStream;
        this.baseFont = AbstractFont.chooseFont(baseFontClassName).getBaseFont();
    }

    @Override
    public void addWatermark(String fileName, OutputStream outputStream, Boolean lock, PdfUser pdfUser) {
        BufferedOutputStream out = null;
        PdfReader reader = null;
        PdfStamper stamper = null;
        FileInputStream fileInputStream = null;
        File file = new File(fileName);
        Random random = new Random();
        try {
            fileInputStream = new FileInputStream(file);
            out = new BufferedOutputStream(outputStream);
            if (lock) {
                reader = new PdfReader(fileInputStream, pdfUser.getAdminPassword().getBytes());
                stamper = new PdfStamper(reader, out);
                pdfUser.lock(stamper, pdfUser.getUserPassword(), pdfUser.getAdminPassword());
            }else {
                reader = new PdfReader(fileInputStream);
                stamper = new PdfStamper(reader, out);
            }

            int total = reader.getNumberOfPages() + 1;
            System.out.println("当前总页数：" + total);
            PdfContentByte content = null;

            // 获取图片文件流
            byte[] bytes = FileUtil.toByteArray(this.watermarkStream);

            Image image = Image.getInstance(bytes);
            image.scaleToFit(200, 150);

            for (int i = 1; i < total; i++) {
                // 水印的起始
                content = stamper.getOverContent(i);
                // 开始
                content.beginText();
                PdfGState pdfGState = new PdfGState();
                pdfGState.setFillOpacity(0.3f);
                content.setGState(pdfGState);
                for (float initX = random.nextInt(200) - 150; initX <= A4.getWidth() + 40; initX += 300) {
                    for (float initY = random.nextInt(200) - 150; initY <= A4.getHeight() + 40; initY += 300) {
                        image.setAbsolutePosition(initX, initY);
                        image.setRotationDegrees(30);
                        content.addImage(image);
                    }
                }
                //结束
                content.endText();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fileInputStream) {
                    fileInputStream.close();
                }
                if (null != stamper) {
                    stamper.close();
                }
                if (null != out) {
                    out.close();
                }
                if (null != reader) {
                    reader.close();
                }
                if (null != file) {
                    file.delete();
                }
                if (null != outputStream) {
                    outputStream.close();
                }
                if (null != this.watermarkStream) {
                    this.watermarkStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
